<?php

/**
 * escalas actions.
 *
 * @package    SistemaRutasEc
 * @subpackage escalas
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class escalasActions extends sfActions {

    public function executeIndex(sfWebRequest $request) {
        $this->idfr = $request->getParameter('id_fr');
        $this->getUser()->setAttribute('id_fr', $this->idfr);
        $q = Doctrine_Query::create()
                ->from('frecuencia f')
                ->where('f.id_fr=?', $this->idfr);
        $this->fre = $q->fetchOne();

        $this->escalas = Doctrine::getTable('escala')->createQuery('e')
                ->where('e.id_fr = ?', $this->idfr)
                ->execute();
    }

    public function executeShow(sfWebRequest $request) {
        $this->escala = Doctrine_Core::getTable('escala')->find(array($request->getParameter('id_es')));
        $this->forward404Unless($this->escala);
    }

    public function executeNew(sfWebRequest $request) {
        $this->fres = $request->getParameter('id_fr');
        $this->getUser()->setAttribute('fres', $this->fres);
        
        $this->form = new escalaForm();
    }

    public function executeCreate(sfWebRequest $request) {
        $this->forward404Unless($request->isMethod(sfRequest::POST));

        $this->form = new escalaForm();

        $this->processFormNew($request, $this->form);

        $this->setTemplate('new');
    }

    public function executeEdit(sfWebRequest $request) {
        $this->forward404Unless($escala = Doctrine_Core::getTable('escala')->find(array($request->getParameter('id_es'))), sprintf('Object escala does not exist (%s).', $request->getParameter('id_es')));
         $this->idfr = $escala->getIdFr();
        
        $this->form = new escalaForm($escala);
    }

    public function executeUpdate(sfWebRequest $request) {
        $this->forward404Unless($request->isMethod(sfRequest::POST) || $request->isMethod(sfRequest::PUT));
        $this->forward404Unless($escala = Doctrine_Core::getTable('escala')->find(array($request->getParameter('id_es'))), sprintf('Object escala does not exist (%s).', $request->getParameter('id_es')));
        $this->form = new escalaForm($escala);

        $this->processFormEdit($request, $this->form);

        $this->setTemplate('edit');
    }

    public function executeDelete(sfWebRequest $request) {
        $request->checkCSRFProtection();

        $this->forward404Unless($escala = Doctrine_Core::getTable('escala')->find(array($request->getParameter('id_es'))), sprintf('Object escala does not exist (%s).', $request->getParameter('id_es')));
        $escala->delete();
        $aux=$escala->getIdFr();
        
        $this->redirect('escalas/index?id_fr=' . $aux);
    }

    protected function processFormNew(sfWebRequest $request, sfForm $form) {
        $form->bind($request->getParameter($form->getName()), $request->getFiles($form->getName()));
        if ($form->isValid()) {            
            $form->getObject()->setIdFr($this->getUser()->getAttribute('fres'));            
            $escala = $form->save();
            $this->redirect('escalas/index?id_fr=' . $escala->getIdFr());                        
        }
    }
    
    
    protected function processFormEdit(sfWebRequest $request, sfForm $form) {
        $form->bind($request->getParameter($form->getName()), $request->getFiles($form->getName()));
        if ($form->isValid()) {            
            
            $escala = $form->save();
            $this->redirect('escalas/index?id_fr=' . $escala->getIdFr());            
        }
    }

}
